<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\common\model\Example;
use app\common\model\Picture;
use app\common\model\Question;
use app\common\model\Article;

class Search extends BaseController
{

    //全局筛选搜索
    public function index()
    {
        $key = input('key');
        $page = input('page/d', 1);
        $type = input('type/d', 1);
        $listRows = 8;
        if (empty($key)) {
            $where['status'] = 1;
            if ($type == 1) {
                $example = Example::where($where)
                    ->field('id,title,image,param,zan_num,col_num')
                    ->page($page, $listRows)
                    ->withAttr('image',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Example::where($where)->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['example' => $example, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
                //素材库
            } elseif ($type == 2) {
                $picture = Picture::where($where)
                    ->field('id,title,image')
                    ->page($page, $listRows)
                    ->withAttr('image',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Picture::where($where)->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['picture' => $picture, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
                //文章
            } elseif ($type == 3) {
                $article = Article::where($where)
                    ->field('id,picture,title,content,zan_num,comment_num,col_num,created_at')
                    ->page($page, $listRows)
                    ->withAttr('picture',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Article::where($where)->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['article' => $article, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
                //问答
            } elseif ($type == 4) {
                $question = Question::where($where)
                    ->field('id,picture,title,content,zan_num,comment_num,col_num,created_at')
                    ->page($page, $listRows)
                    ->withAttr('picture',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Question::where($where)->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['question' => $question, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
            }
        } else {
            //实例
            if ($type == 1) {
                $example = Example::where('title|content|hou_content|qian_content', 'like', '%' . $key . '%')
                    ->field('id,title,image,param,zan_num,col_num')
                    ->page($page, $listRows)
                    ->withAttr('image',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Example::where('title|content|hou_content|qian_content', 'like', '%' . $key . '%')->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['example' => $example, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
                //素材库
            } elseif ($type == 2) {
                $picture = Picture::where('title|content', 'like', '%' . $key . '%')
                    ->field('id,title,image')
                    ->page($page, $listRows)
                    ->withAttr('image',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Picture::where('title|content', 'like', '%' . $key . '%')->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['picture' => $picture, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
                //文章
            } elseif ($type == 3) {
                $article = Article::where('title|content', 'like', '%' . $key . '%')
                    ->field('id,picture,title,content,zan_num,comment_num,col_num,created_at')
                    ->page($page, $listRows)
                    ->withAttr('picture',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Article::where('title|content', 'like', '%' . $key . '%')->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['article' => $article, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
                //问答
            } elseif ($type == 4) {
                $question = Question::where('title|content', 'like', '%' . $key . '%')
                    ->field('id,picture,title,content,zan_num,comment_num,col_num,created_at')
                    ->page($page, $listRows)
                    ->withAttr('picture',function ($val,$data){
                        return env('app.yuming') . $val;
                    })
                    ->select();
                $count = Question::where('title|content', 'like', '%' . $key . '%')->count();
                $maxpage = ceil($count / $listRows);
                return sucessMsg(200, '搜索成功', ['question' => $question, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count]);
            }
        }
    }
}
